package com.game.majiang.algorithm.chexuan;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.game.majiang.algorithm.PaperCard;
import com.game.majiang.algorithm.PaperSortByNum;
import com.game.majiang.config.LogManager;

/**
 * 游戏算法
 */

public class GameAlgorithm {
	
	//红对子
	public static final ChexuanCardType[] REDD = {ChexuanCardType.DIPAI, ChexuanCardType.HEPAI, ChexuanCardType.MAOMAO, ChexuanCardType.GAOYAO, ChexuanCardType.RENPAI, 
			ChexuanCardType.SHAOSHI, ChexuanCardType.TIANPAI};
	//黑对子
	public static final ChexuanCardType[] BLACKD = {ChexuanCardType.BANDENG, ChexuanCardType.BAOZI_5, ChexuanCardType.CHANGSAN, ChexuanCardType.BAOZI_7,
			ChexuanCardType.BAOZI_8, ChexuanCardType.BAOZI_9, ChexuanCardType.MEISHI, ChexuanCardType.HUTOU};
	
	public static ArrayList<ArrayList<PaperCard>> initCardGroup() {
		ArrayList<PaperCard> listPC = initCard();
		ArrayList<ArrayList<PaperCard>> listPCs = new ArrayList<ArrayList<PaperCard>>();
		for (int i = 0; i < 8; i++) {
			ArrayList<PaperCard> list = new ArrayList<PaperCard>();
			for (int j = 0; j < 4; j++) {
				list.add(getCard(listPC,0,0));
			}
			listPCs.add(list);
		}
		return listPCs;
	}
	
	public static ArrayList<PaperCard> initCard(){
		ArrayList<PaperCard> listPC = new ArrayList<PaperCard>();
		int id = 0;
		for (int i = 1; i < 5; i++) {
			for (int j = 2; j < 13; j++) {
				if((i % 2 == 0 && (j == 5)) || (i % 2 == 1 && (j == 2 || j == 12)) || ( i % 2 == 0 && (j == 9 || j == 11))) {
					continue;
				}
				if(j == 3 && i != 2) {
					continue;
				}
				id ++;
				PaperCard pc = new PaperCard();
				pc.initChexuanCard(id, i, j);
				listPC.add(pc);
			}
		}
		id ++;
		PaperCard pc = new PaperCard();
		pc.initChexuanCard(id, 0, 15);
		listPC.add(pc);
		Collections.shuffle(listPC);
		return listPC;
	}

	/**
	 * 初始化所有玩家卡牌，并给指定座位初始指定卡牌
	 * @param list 指定座位的卡牌
	 * @param seatNo 座位号
	 * @return
	 */
	public static ArrayList<ArrayList<PaperCard>> initCardGroup(ArrayList<PaperCard> list, int seatNo) {
		ArrayList<PaperCard> initCard = initCard();
		ArrayList<ArrayList<PaperCard>> listPCs = new ArrayList<ArrayList<PaperCard>>();
		ArrayList<PaperCard> listPC_Cheat = new ArrayList<>();
		if(seatNo >= 0) {
			for (PaperCard paperCard : list) {
				listPC_Cheat.add(getCard(initCard, paperCard.getNum(), paperCard.getType()));
			}
		}
		// 共8个座位，每个位置4张牌
		for (int i = 0; i < 8; i++) {
			if(seatNo != i) {
				ArrayList<PaperCard> listPC = new ArrayList<PaperCard>();
				for (int j = 0; j < 4; j++) {
					listPC.add(getCard(initCard,0,0));
				}
				listPCs.add(listPC);
			}
		}
		if(seatNo >= 0) {
			LogManager.getInstance().Log("为座位" + seatNo + " 初始化卡牌: " + listPC_Cheat);
			listPCs.add(seatNo, listPC_Cheat);
		}
		return listPCs;
	}
	/**
	 * 配牌
	 */
	public static ArrayList<ArrayList<PaperCard>> matchCards() {
		ArrayList<PaperCard> initCard = initCard();
		ArrayList<ArrayList<PaperCard>> listPCs = new ArrayList<ArrayList<PaperCard>>();
		ArrayList<PaperCard> listPC_0 = new ArrayList<PaperCard>();
		listPC_0.add(getCard(initCard,10,1));
		listPC_0.add(getCard(initCard,10,2));
		listPC_0.add(getCard(initCard,11,3));
		listPC_0.add(getCard(initCard,0,0));
		listPCs.add(listPC_0);
		ArrayList<PaperCard> listPC_1 = new ArrayList<PaperCard>();
		listPC_1.add(getCard(initCard,3,2));
		listPC_1.add(getCard(initCard,6,1));
		listPC_1.add(getCard(initCard,11,1));
		listPC_1.add(getCard(initCard,7,3));
		listPCs.add(listPC_1);
		ArrayList<PaperCard> listPC_2 = new ArrayList<PaperCard>();
		listPC_2.add(getCard(initCard,9,1));
		listPC_2.add(getCard(initCard,12,2));
		listPC_2.add(getCard(initCard,9,3));
		listPC_2.add(getCard(initCard,12,4));
		listPCs.add(listPC_2);
		ArrayList<PaperCard> listPC_3 = new ArrayList<PaperCard>();
		listPC_3.add(getCard(initCard,0,0));
		listPC_3.add(getCard(initCard,0,0));
		listPC_3.add(getCard(initCard,0,0));
		listPC_3.add(getCard(initCard,0,0));
		listPCs.add(listPC_3);
		ArrayList<PaperCard> listPC_4 = new ArrayList<PaperCard>();
		listPC_4.add(getCard(initCard,0,0));
		listPC_4.add(getCard(initCard,0,0));
		listPC_4.add(getCard(initCard,0,0));
		listPC_4.add(getCard(initCard,0,0));
		listPCs.add(listPC_4);
		ArrayList<PaperCard> listPC_5 = new ArrayList<PaperCard>();
		listPC_5.add(getCard(initCard,0,0));
		listPC_5.add(getCard(initCard,0,0));
		listPC_5.add(getCard(initCard,0,0));
		listPC_5.add(getCard(initCard,0,0));
		listPCs.add(listPC_5);
		ArrayList<PaperCard> listPC_6 = new ArrayList<PaperCard>();
		listPC_6.add(getCard(initCard,0,0));
		listPC_6.add(getCard(initCard,0,0));
		listPC_6.add(getCard(initCard,0,0));
		listPC_6.add(getCard(initCard,0,0));
		listPCs.add(listPC_6);
		ArrayList<PaperCard> listPC_7 = new ArrayList<PaperCard>();
		listPC_7.add(getCard(initCard,0,0));
		listPC_7.add(getCard(initCard,0,0));
		listPC_7.add(getCard(initCard,0,0));
		listPC_7.add(getCard(initCard,0,0));
		listPCs.add(listPC_7);
		return listPCs;
	}
	public static PaperCard getCard(ArrayList<PaperCard> listPC, int num, int type) {
		if(listPC.size() > 0 &&  num == 0 && type == 0) {
			 return listPC.remove(0);
		}
		for (int i = 0; i < listPC.size(); i++) {
			PaperCard paperCard = listPC.get(i);
			if(paperCard.getNum() == num && paperCard.getType() == type) {
				return listPC.remove(i);
			}
		}
		return null;
	}
	
	
	public static ChexuanCardType checkCardType(List<PaperCard> listCards, boolean diwangBool) {
		ArrayList<PaperCard> copyCards = new ArrayList<PaperCard>(listCards);
//		for (PaperCard paperCard : listCards) {
//			copyCards.add(paperCard);
//		}
		copyCards.sort(new PaperSortByNum());
		PaperCard pc1 = copyCards.get(0);
		PaperCard pc2 = copyCards.get(1);
		if(pc1.getNum() == pc2.getNum() && pc1.getType() % 2 == pc2.getType() % 2) {
			if(pc1.getType() % 2 == 0) {
				int[] num = {2,4,6,7,8,10,12};
				for (int i = 0; i < num.length; i++) {
					if(pc1.getNum() == num[i]) {
						return REDD[i];
					}
				}
			} else {
				int[] num = {4,5,6,7,8,9,10,11};
				for (int i = 0; i < num.length; i++) {
					if(pc1.getNum() == num[i]) {
						return BLACKD[i];
					}
				}
			}
		}
		if(pc1.getNum() == 3 && pc2.getNum() == 15) return ChexuanCardType.DINGHUANG;
		if(pc1.getNum() == 9 && pc2.getNum() == 12 && pc2.getType() % 2 == 0) return ChexuanCardType.TIANWANG;
		if(diwangBool && pc1.getNum() == 2 && pc1.getType() % 2 == 0 && pc2.getNum() == 9 && pc2.getType() % 2 == 1) return ChexuanCardType.DIWANG;
		if(pc1.getNum() == 8 && pc2.getNum() == 12 && pc2.getType() % 2 == 0) return ChexuanCardType.TIANGANG;
		if(pc1.getNum() == 2 && pc1.getType() % 2 == 0 && pc2.getNum() == 8 ) return ChexuanCardType.DIGANG;
		if(pc1.getNum() == 7 && pc2.getNum() == 12 ) return ChexuanCardType.TIANGUANJIU;
		if(pc1.getNum() == 2 && pc2.getNum() == 7 ) return ChexuanCardType.DIGUANJIU;
		if(pc1.getNum() == 8 && pc1.getType() % 2 == 0 && pc2.getNum() == 11 ) return ChexuanCardType.DENGLONGJIU;
		if(pc1.getNum() == 4 && pc1.getType() % 2 == 0 && pc2.getNum() == 5 ) return ChexuanCardType.HEWUJIU;
		if(pc1.getNum() == 4 && pc1.getType() % 2 == 1 && pc2.getNum() == 5 ) return ChexuanCardType.BANWUJIU;
		if(pc1.getNum() == 3 && pc1.getType() % 2 == 0 && pc2.getNum() == 6 && pc2.getType() % 2 == 1 ) return ChexuanCardType.DINGCHANGJIU;
		if(pc1.getNum() == 9 && pc2.getNum() == 10 && pc2.getType() % 2 == 1) return ChexuanCardType.MEISHIJIU;
		if(pc1.getNum() == 3 && pc1.getType() % 2 == 0 && pc2.getNum() == 6 && pc2.getType() % 2 == 0 ) return ChexuanCardType.DINGMAOJIU;
		if(pc1.getNum() == 8 && pc1.getType() % 2 == 1 && pc2.getNum() == 11 ) return ChexuanCardType.WULONGJIU;
		if(pc1.getNum() == 9 && pc2.getNum() == 10 && pc2.getType() % 2 == 0) return ChexuanCardType.SHAOSHIJIU;
		switch ((pc1.getPoint() + pc2.getPoint()) % 10) {
		case 9:
			return ChexuanCardType.ZAPAI_9;
		case 8:
			return ChexuanCardType.ZAPAI_8;
		case 7:
			return ChexuanCardType.ZAPAI_7;
		case 6:
			return ChexuanCardType.ZAPAI_6;
		case 5:
			return ChexuanCardType.ZAPAI_5;
		case 4:
			return ChexuanCardType.ZAPAI_4;
		case 3:
			return ChexuanCardType.ZAPAI_3;
		case 2:
			return ChexuanCardType.ZAPAI_2;
		case 1:
			return ChexuanCardType.ZAPAI_1;
		case 0:
			return ChexuanCardType.ZAPAI_0;
		default:
			break;
		}
		return ChexuanCardType.ZAPAI_0;
	}
	
	public static int checkSanhua(ArrayList<PaperCard> listCards) {
		if(checkSanhuaSix(listCards)) {
			return 6;
		}
		if(checkSanhuaTen(listCards)) {
			return 10;
		}
		return 0;
	}
	public static boolean checkSanhuaSix(ArrayList<PaperCard> listCards) {
		int count = 0;
		for (PaperCard paperCard : listCards) {
			if(paperCard.getNum() == 6 &&  paperCard.getType() % 2 == 1) {
				count ++;
			}
		}
		if(count != 1) {
			return false;
		}
		count = 0;
		for (PaperCard paperCard : listCards) {
			if(paperCard.getNum() == 6 &&  paperCard.getType() % 2 == 0) {
				count ++;
			}
		}
		if(count != 1) {
			return false;
		}
		count = 0;
		for (PaperCard paperCard : listCards) {
			if(paperCard.getNum() == 15 ) {
				count ++;
			}
		}
		return count == 1;
	}
	public static boolean checkSanhuaTen(ArrayList<PaperCard> listCards) {
		int count = 0;
		for (PaperCard paperCard : listCards) {
			if(paperCard.getNum() == 10 &&  paperCard.getType() % 2 == 1) {
				count ++;
			}
		}
		if(count != 1) {
			return false;
		}
		count = 0;
		for (PaperCard paperCard : listCards) {
			if(paperCard.getNum() == 10 &&  paperCard.getType() % 2 == 0) {
				count ++;
			}
		}
		if(count != 1) {
			return false;
		}
		count = 0;
		for (PaperCard paperCard : listCards) {
			if(paperCard.getNum() == 11 &&  paperCard.getType() % 2 == 1) {
				count ++;
			}
		}
		return count == 1;
	}
	
	public static ArrayList<PaperCard> splitHeadTail(ArrayList<PaperCard> listCards, boolean diwangBool) {
		ArrayList<PaperCard> list = new ArrayList<PaperCard>();
//		ArrayList<PaperCard> checkDingerHuang = checkDingerHuang(listCards);
//		if(checkDingerHuang.size() == 2) {
//			list.addAll(checkDingerHuang);
//			for (PaperCard paperCard : listCards) {
//				if(paperCard.getNum() != 3 && paperCard.getNum() != 15) {
//					list.add(paperCard);
//				}
//			}
//		} else {}

		List<PaperCard> listFront = listCards.subList(0, 2);
		List<PaperCard> listAfter = listCards.subList(2, 4);
		ChexuanCardType checkCardType_Front = checkCardType(listFront, diwangBool);
		ChexuanCardType checkCardType_After = checkCardType(listAfter, diwangBool);
		if(checkCardType_Front.getValue() > checkCardType_After.getValue()) {
			list.addAll(listFront);
			list.addAll(listAfter);
		} else if(checkCardType_Front.getValue() == checkCardType_After.getValue()) {
			PaperCard checkCardsSingleMax_Front_Tou = checkCardsSingleMax(listFront);
			PaperCard checkCardsSingleMax_After_Tou = checkCardsSingleMax(listAfter);
			if(checkCardsSingleMax_Front_Tou.getChexuanValue() >= checkCardsSingleMax_After_Tou.getChexuanValue()) {
				list.addAll(listFront);
				list.addAll(listAfter);
			} else {
				list.addAll(listAfter);
				list.addAll(listFront);
			}
		} else {
			list.addAll(listAfter);
			list.addAll(listFront);
		} 
	
		return list;
	}
	/**
	 * 
	 * @param frontListCards
	 * @param afterListCards
	 * @param dijiuWangBool
	 * @return 0 头小尾小 1头小尾平 2 头小尾大 3头平尾小 4头平尾平 5头平尾大 6 头大尾小 7头大尾平 8头大尾大
	 */
	public static int compareCardBig(ArrayList<PaperCard> frontListCards, ArrayList<PaperCard> afterListCards, boolean dijiuWangBool) {
		List<PaperCard> subList_Front_Tou = frontListCards.subList(0, 2);
		ChexuanCardType checkCardType_Front_Tou = GameAlgorithm.checkCardType(subList_Front_Tou, dijiuWangBool);
		List<PaperCard> subList_Front_Wei =  frontListCards.subList(2, 4);
		ChexuanCardType checkCardType_Front_Wei = GameAlgorithm.checkCardType(subList_Front_Wei, dijiuWangBool);
		List<PaperCard> subList_After_Tou =  afterListCards.subList(0, 2);
		ChexuanCardType checkCardType_After_Tou = GameAlgorithm.checkCardType(subList_After_Tou, dijiuWangBool);
		List<PaperCard> subList_After_Wei = afterListCards.subList(2, 4);
		ChexuanCardType checkCardType_After_Wei = GameAlgorithm.checkCardType(subList_After_Wei, dijiuWangBool);
		int value = -1;
		if(checkCardType_Front_Tou.getValue() > checkCardType_After_Tou.getValue()) {
			value = compareTouBig(checkCardType_Front_Wei, subList_Front_Wei, checkCardType_After_Wei, subList_After_Wei);
		} else if(checkCardType_Front_Tou.getValue() == checkCardType_After_Tou.getValue()) {
			PaperCard checkCardsSingleMax_Front_Tou = checkCardsSingleMax(subList_Front_Tou);
			PaperCard checkCardsSingleMax_After_Tou = checkCardsSingleMax(subList_After_Tou);
			if(checkCardsSingleMax_Front_Tou.getChexuanValue() > checkCardsSingleMax_After_Tou.getChexuanValue()) {
				value = compareTouBig(checkCardType_Front_Wei, subList_Front_Wei, checkCardType_After_Wei, subList_After_Wei);
			} else if(checkCardsSingleMax_Front_Tou.getChexuanValue() == checkCardsSingleMax_After_Tou.getChexuanValue()) {
				value = compareTouFlat(checkCardType_Front_Wei, subList_Front_Wei, checkCardType_After_Wei, subList_After_Wei);
			} else {
				value = compareTouSmall(checkCardType_Front_Wei, subList_Front_Wei, checkCardType_After_Wei, subList_After_Wei);
			}
		} else {
			value = compareTouSmall(checkCardType_Front_Wei, subList_Front_Wei, checkCardType_After_Wei, subList_After_Wei);
		}
//		LogManager.getInstance().Log("subList_Front_Tou:"+subList_Front_Tou.toString()+" checkCardType_Front_Tou:"+checkCardType_Front_Tou.getName());
//		LogManager.getInstance().Log("subList_Front_Wei:"+subList_Front_Wei.toString()+" checkCardType_Front_Wei:"+checkCardType_Front_Wei.getName());
//		LogManager.getInstance().Log("subList_After_Tou:"+subList_After_Tou.toString()+" checkCardType_After_Tou:"+checkCardType_After_Tou.getName());
//		LogManager.getInstance().Log("subList_After_Wei:"+subList_After_Wei.toString()+" checkCardType_After_Wei:"+checkCardType_After_Wei.getName());
//		LogManager.getInstance().Log("value:"+value);
		return value;
	}
	/**
	 * 头小
	 * @return  0 头小尾小 1头小尾平 2 头小尾大
	 */
	public static int compareTouSmall(ChexuanCardType checkCardType_Front_Wei, List<PaperCard> subList_Front_Wei, ChexuanCardType checkCardType_After_Wei, List<PaperCard> subList_After_Wei) {
		int value = -1;
		if(checkCardType_Front_Wei.getValue() > checkCardType_After_Wei.getValue()) {
			value = 2;
		} else if(checkCardType_Front_Wei.getValue() == checkCardType_After_Wei.getValue()) {
			PaperCard checkCardsSingleMax_Front_Wei = checkCardsSingleMax(subList_Front_Wei);
			PaperCard checkCardsSingleMax_After_Wei = checkCardsSingleMax(subList_After_Wei);
			if(checkCardsSingleMax_Front_Wei.getChexuanValue() > checkCardsSingleMax_After_Wei.getChexuanValue()) {
				value = 2;
			} else if(checkCardsSingleMax_Front_Wei.getChexuanValue() == checkCardsSingleMax_After_Wei.getChexuanValue()) {
				value = 1;
			} else {
				value = 0;
			}
		} else {
			value = 0;
		}
		return value;
	}
	/**
	 * 头平
	 * @return  3头平尾小 4头平尾平 5头平尾大
	 */
	public static int compareTouFlat(ChexuanCardType checkCardType_Front_Wei, List<PaperCard> subList_Front_Wei, ChexuanCardType checkCardType_After_Wei, List<PaperCard> subList_After_Wei) {
		int value = -1;
		if(checkCardType_Front_Wei.getValue() > checkCardType_After_Wei.getValue()) {
			value = 5;
		} else if(checkCardType_Front_Wei.getValue() == checkCardType_After_Wei.getValue()) {
			PaperCard checkCardsSingleMax_Front_Wei = checkCardsSingleMax(subList_Front_Wei);
			PaperCard checkCardsSingleMax_After_Wei = checkCardsSingleMax(subList_After_Wei);
			if(checkCardsSingleMax_Front_Wei.getChexuanValue() > checkCardsSingleMax_After_Wei.getChexuanValue()) {
				value = 5;
			} else if(checkCardsSingleMax_Front_Wei.getChexuanValue() == checkCardsSingleMax_After_Wei.getChexuanValue()) {
				value = 4;
			} else {
				value = 3;
			}
		} else {
			value = 3;
		}
		return value;
	}
	/**
	 * 头大 
	 * @return  6 头大尾小 7头大尾平 8头大尾大
	 */
	public static int compareTouBig(ChexuanCardType checkCardType_Front_Wei, List<PaperCard> subList_Front_Wei, ChexuanCardType checkCardType_After_Wei, List<PaperCard> subList_After_Wei) {
		int value = -1;
		if(checkCardType_Front_Wei.getValue() > checkCardType_After_Wei.getValue()) {
			value = 8;
		} else if(checkCardType_Front_Wei.getValue() == checkCardType_After_Wei.getValue()) {
			PaperCard checkCardsSingleMax_Front_Wei = checkCardsSingleMax(subList_Front_Wei);
			PaperCard checkCardsSingleMax_After_Wei = checkCardsSingleMax(subList_After_Wei);
			if(checkCardsSingleMax_Front_Wei.getChexuanValue() > checkCardsSingleMax_After_Wei.getChexuanValue()) {
				value = 8;
			} else if(checkCardsSingleMax_Front_Wei.getChexuanValue() == checkCardsSingleMax_After_Wei.getChexuanValue()) {
				value = 7;
			} else {
				value = 6;
			}
		} else {
			value = 6;
		}
		return value;
	}
	
	public static ArrayList<PaperCard> checkDingerHuang(List<PaperCard> listCards) {
		ArrayList<PaperCard> listHuang = new ArrayList<PaperCard>();
		for (PaperCard paperCard : listCards) {
			if(paperCard.getNum() == 3 || paperCard.getNum() == 15) {
				listHuang.add(paperCard);
			}
		}
		return listHuang;
	}
	
	public static PaperCard checkCardsSingleMax(List<PaperCard> listCards) {
		if(listCards.get(0).getChexuanValue() > listCards.get(1).getChexuanValue()) {
			return listCards.get(0);
		} else {
			return listCards.get(1);
		}
	}
	
}




